package com.example.littleken.ormlite_text.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.littleken.ormlite_text.R;

/**
 * 闪屏页Acitvity
 */
public class SplashActivity extends AppCompatActivity {
    //闪屏页停留时间
    public static final int WAIT_SECOND = 2;
   /* private Handler mHandler = new Handler();*/

    private TextView textView;
    private Button btn_clear;
    //Flag,用于判断app是否第一次启动
    public static final String GUILDFLAG = "guild";
   //在存储写入信息，用于判断app是否第一次启动
    SharedPreferences sharedPreferences ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //清除存储中的信息，呼出引导页，用于调试
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* Toast.makeText(SplashActivity.this,"引导页",Toast.LENGTH_SHORT).show();*/
                sharedPreferences = getSharedPreferences(GUILDFLAG, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(GUILDFLAG,"");
                editor.commit();
            }
        });
        //闪屏页倒计时
        textView = (TextView) findViewById(R.id.tv_second);
        textView.setText(WAIT_SECOND + "");
        AsyncTask asyncTask = new SplahActivityWait().execute(WAIT_SECOND);
        /*mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        }, 3000);*/
    }

    //多线程，用于闪屏页倒计时，倒计时结束后进行页面跳转
    class SplahActivityWait extends AsyncTask<Integer,Integer,Boolean>{


        @Override
        protected Boolean doInBackground(Integer... integers) {
            try {
                //倒计时
                for(int i = integers[0];i>=0;i--){
                    Thread.sleep(1000);
                    publishProgress(i);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            //倒计时
            textView.setText(values[0]+"");
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            sharedPreferences = getSharedPreferences(GUILDFLAG, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            //通过判断存储中的信息，来确定app是否第一次启动
            String flag = sharedPreferences.getString(GUILDFLAG,null);
            if(flag==null||flag.equals("")){
                editor.putString(GUILDFLAG,GUILDFLAG);
                editor.commit();
                startActivity(new Intent(SplashActivity.this,GuideActivity.class));
                finish();
            }else{
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        }
    }
}
